/*
 * (C) Copyright 2014-2016, by Dimitrios Michail
 *
 * JHeaps Library
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jheaps.array

import org.jheaps.Constants
import org.jheaps.annotations.ConstantTime
import org.jheaps.annotations.LogarithmicTime

/**
 * Abstract implementation of a heap using an array representation.
 *
 * @author Dimitrios Michail
 *
 * @param <K>
 * the type of keys maintained by this heap
</K> */
abstract class AbstractArrayHeap<K>
    (comparator: Comparator<in K?>?, capacity: Int) : AbstractArrayWeakHeap<K>(comparator, capacity) {
    /**
     * Initialize the array representation
     */
    override fun initCapacity(capacity: Int) {
        this.array = arrayOfNulls<Any>(capacity + 1) as Array<K?>
    }

    /**
     * {@inheritDoc}
     */
    @ConstantTime
    override fun findMin(): K {
        if (Constants.NOT_BENCHMARK && size == 0) {
            throw NoSuchElementException()
        }
        return array[1]!!
    }

    /**
     * {@inheritDoc}
     */
    @LogarithmicTime(amortized = true)
    override fun insert(key: K) {
        if (Constants.NOT_BENCHMARK) {
            if (key == null) {
                throw NullPointerException("Null keys not permitted")
            }
            // make sure there is space
            if (size == array.size - 1) {
                if (array.size == 1) {
                    ensureCapacity(1)
                } else {
                    ensureCapacity(2 * (array.size - 1))
                }
            }
        }

        array[++size] = key

        if (comparator == null) {
            fixup(size)
        } else {
            fixupWithComparator(size)
        }
    }

    /**
     * {@inheritDoc}
     */
    @LogarithmicTime(amortized = true)
    override fun deleteMin(): K {
        if (Constants.NOT_BENCHMARK && size == 0) {
            throw NoSuchElementException()
        }

        val result = array[1]
        if (size == 1) {
            array[1] = null
            size = 0
        } else {
            array[1] = array[size]
            array[size] = null
            size--
            if (comparator == null) {
                fixdown(1)
            } else {
                fixdownWithComparator(1)
            }
        }

        if (Constants.NOT_BENCHMARK) {
            // free unused space
            val currentCapacity: Int = array.size - 1
            if (2 * minCapacity <= currentCapacity && 4 * size < currentCapacity) {
                ensureCapacity(currentCapacity / 2)
            }
        }

        return result!!
    }


}
